var $ = require('./util'), api = require('./api'), notice = require("./notice");
module.exports = {
  copy: function (e) {
    wx.setClipboardData({
      data: e.currentTarget.dataset.num,
      success: function (res) {
        wx.showToast({
          title: '复制成功',
          icon: 'success',
          duration: 1000
        })
      }
    })
  },
  moretx: function (t) {
    if (t.data.ishdtpl && t.data.status == 0) return;
    $.gopage('../canyus/canyus')
  },
  dizhi: function (t) {
    var td = t.data
    wx.openLocation({
      latitude: td.g.store.latitude,
      longitude: td.g.store.longitude,
      name: "",
      scale: 14,
      address: td.g.store.address,
    })
  },
  countDown: function (t) {
    // 获取当前时间，同时得到活动结束时间数组
    var newTime = new Date().getTime();
    var td = t.data;
    // 对结束时间进行处理渲染到页面
    var endTime = new Date(td.dateEnd).getTime();
    if (!endTime && td.dateEnd) endTime = new Date(td.dateEnd.replace(/-/g, "/")).getTime();
    var obj = null;
    // 如果活动未结束，对时间进行处理
    if (endTime && endTime - newTime > 0) {
      var time = (endTime - newTime) / 1000;
      // 获取天、时、分、秒
      var day = parseInt(time / (60 * 60 * 24));
      var hou = parseInt(time % (60 * 60 * 24) / 3600);
      var min = parseInt(time % (60 * 60 * 24) % 3600 / 60);
      var sec = parseInt(time % (60 * 60 * 24) % 3600 % 60);
      obj = $.timeFormat(day) + '天' + $.timeFormat(hou) + '时' + $.timeFormat(min) + '分' + $.timeFormat(sec) + '秒'
    } else {
      //活动已结束，全部设置为'00'
      obj = '00天00时00分00秒'
    }
    t.setData({countDown: obj})
  },
  bofang: function (t) {
    var td = t.data, url = td.video, vix = td.vix;
    t.setData({vix: true})
    var videoContext = wx.createVideoContext('url')
    videoContext.requestFullScreen();
    videoContext.play()
  },
  toCollect: function (t, e) {
    var td = t.data, ck = e.currentTarget.dataset.ck, sctype = e.currentTarget.dataset.type
    t.setData({sctype: sctype})
    $.isAuth() ? t.collect(t, sctype) : (notice.addNotification("collect", t.collect, t), t.setData({
      show: !td.show,
      ck: ck,
      sctype: sctype
    }))
  },
  collect: function (t) {
    var g = t.data.g, sctype = t.data.sctype
    var p = sctype == 1 ? {gid: g.id, price: g.saleprice, pic: g.pic, name: g.name} : {
      gid: g.sid, price: g.saleprice, pic: g.store.pic, name: g.store.name
    }
    t.setData({display: false})
    p.type = sctype
    if (sctype == 1) {
      if (t.data.shoucang) {
        $.post(api.DelAttention, p, function (e) {
          t.setData({shoucang: !1, sctype: sctype})
          $.alert("已取消收藏！")
        })
      } else {
        $.post(api.AddAttention, p, function (e) {
          t.setData({shoucang: !0, sctype: sctype})
          $.alert("已收藏！")
        })
      }
    } else {
      if (t.data.dpshoucang) {
        $.post(api.DelAttention, p, function (e) {
          t.setData({dpshoucang: !1, sctype: sctype})
          $.alert("已取消收藏！")
        })
      } else {
        $.post(api.AddAttention, p, function (e) {
          t.setData({dpshoucang: !0, sctype: sctype})
          $.alert("已收藏！")
        })
      }
    }
  },
  shangxiajia: function (t) {
    var td = t.data, state = td.state, p = {id: td.gid, state: state * -1};
    $.confirm('是否' + (state == 1 ? '下架' : '上架') + '该活动', true, function (e) {
      e.confirm && $.post(api.xiugai, p, function (e) {
        e.code == 0 ? $.redirect("/spack/huodonggl/huodonggl") : $.alert(e.msg)
      })
    })
  },
  tongguo: function (t) {
    var td = t.data, p = {gid: td.gid, status: 2};
    t.setData({LD: !0})
    $.confirm("是否审核通过？", true, function (e) {
      e.confirm && $.post(api.huodongshenhe, p, function (e) {
        e.code == 0 ? $.redirect("/spack/huodongsh/huodongsh") : $.alert(e.msg)
      })
    })
  },
  jujue: function (t) {
    var td = t.data, p = {gid: td.gid, status: -1};
    t.setData({LD: !0})
    $.confirm("是否拒绝该活动？", true, function (e) {
      e.confirm && $.post(api.huodongshenhe, p, function (e) {
        e.code == 0 ? $.redirect("/spack/huodongsh/huodongsh") : $.alert(e.msg)
      })
    })
  },
  shangjiainfo: function (e) {
    var sid = e.currentTarget.dataset.sid
    var stype = e.currentTarget.dataset.stype
    if (sid == 3766) {
      $.gopage('/pages/category/category')
      $.gotab('/pages/category/category')
    } else if (stype == 4842 || stype == '4842') {
      return;
    } else if (sid > 0) {
      $.gopage('/pages/mdetail/mdetail?sid=' + sid)
    } else {
      $.alertModal('此活动为平台发布活动，暂无门店')
    }
  },
  toKaituan: function (t, e) {
    var td = t.data, ed = e.currentTarget.dataset, ck = ed.ck;
    if (td.status == 0) return;
    $.isAuth() ? t.kaituan() : (notice.addNotification("kaituan", t.kaituan, t), t.setData({
      show: !td.show,
      ck: ck
    }))
  },
  kaituan: function (t) {
    $.gopage("/pages/kaituan/kaituan?gid=" + t.data.gid + '&type=' + t.data.type)
  },
  music: function (mp3, t) {
    // 对实例进行设置
    mp3.src = t.data.musicurl
    mp3.title = 'Tassel'   // 标题为必选项
    mp3.play()               // 开始播放
  },
  stop: function (t, mp3) {
    var td = t.data, on = td.on;
    if (on == true) {
      mp3.pause()
      t.setData({on: false})
    } else {
      mp3.play()
      t.setData({on: true})
    }
  },
  pageScroll: function (t, e) {
    var kdshow = t.data.kdshow
    if (!kdshow && e.scrollTop * 2 >= 700) t.setData({kdshow: !0})
    if (kdshow && e.scrollTop * 2 < 700) t.setData({kdshow: !1})
  },
  tofxpyq: function (t, e) {
    var ck = e.currentTarget.dataset.ck
    $.isAuth() ? t.fxpyq() : (notice.addNotification("fxpyq", t.fxpyq, t), t.setData({show: true, ck: ck}))
  },
  fxpyq: function (t) {
    var td = t.data, g = td.g, title = g.name,
          p = {ywid: g.id, page: $.pt(g.type), withparam: true, oid: td.oid, type: td.type}, sp = g.saleprice + "",
          pn = $.pn(g.type);
    wx.showLoading({
      title: '生成分享图中',
      mask: true
    })
    var left = sp.length <= 2 ? 90 : (sp.length - 2) * 15 + 90
    var saleprice = g.saleprice < 0 ? '面议' : '￥' + g.saleprice
    var price = g.price < 0 ? '面议' : '原价:￥' + g.price
    $.get(api.goodspyq, p, function (e) {
      var painting = {
        width: 375,
        height: 555,
        clear: true,
        views: [
          {
            type: 'image',
            url: 'https://imgcdn.niucaiduo.com/huakouyun/huakouyun/Resource/755808a538c5a9bc.jpg',
            top: 0,
            left: 0,
            width: 375,
            height: 555
          },
          {
            type: 'image',
            url: $.avatar(),
            top: 15,
            left: 29,
            width: 55,
            height: 55
          },
          // {
          //     type: 'image',
          //     url: 'https://imgcdn.niucaiduo.com/bg/logobg.jpeg',
          //     top: 15,
          //     left: 29,
          //     width: 55,
          //     height: 55
          // },
          {
            type: 'text',
            content: '您的好友【' + $.name() + '】',
            fontSize: 16,
            color: '#402D16',
            textAlign: 'left',
            top: 21,
            left: 96,
            bolder: true
          },
          {
            type: 'text',
            content: '邀请你参加' + (g.store.jianchen || g.store.name) + '的' + pn + '活动！',
            fontSize: 15,
            color: '#563D20',
            textAlign: 'left',
            top: 47,
            left: 96
          },
          {
            type: 'image',
            url: g.pic,
            top: 100,
            left: 51,
            width: 280,
            height: 280
          },
          {
            type: 'image',
            url: e.info,
            top: 445,
            left: 65,
            width: 78,
            height: 78
          },
          {
            type: 'text',
            content: title,
            fontSize: 16,
            lineHeight: 21,
            color: '#383549',
            textAlign: 'left',
            top: 398,
            left: 44,
            width: 287,
            MaxLineNumber: 1,
            breakWord: true,
            bolder: true
          },
          {
            type: 'text',
            content: saleprice,
            fontSize: 19,
            color: '#E62004',
            textAlign: 'left',
            top: 420,
            left: 44.5,
            bolder: true
          },
          {
            type: 'text',
            content: price,
            fontSize: 13,
            color: '#7E7E8B',
            textAlign: 'left',
            top: 425,
            left: left,
            textDecoration: 'line-through'
          },
          {
            type: 'text',
            content: '长按识别图中二维码立即参加',
            fontSize: 14,
            color: '#383549',
            textAlign: 'left',
            top: 470,
            left: 165.5,
            lineHeight: 20,
            MaxLineNumber: 2,
            breakWord: true,
            width: 125
          }
        ]
      }
      t.setData({pyq: !0, painting: painting})
    })
  },
  eventGetImage: function (t, e) {
    var imgs = [], p = e.detail.tempFilePath
    imgs.push(p)
    t.setData({pyqurl: p, pyq: !1})
    wx.previewImage({current: p, urls: imgs})
  },
  onload: function (t, e) {
    $.showmenu()
    var scene = e.scene ? decodeURIComponent(e.scene) : "", gid = e.gid || e.id, oid = e.oid || '', type;
    if (e.ori) {
      $.delcache('scene')
      $.scache('ori', e.ori)
    }
    if (scene.length > 0) {
      $.delcache('ori')
      $.scache('scene', scene)
      var scenes = scene.split("_")
      if (scenes.length >= 4) oid = scenes[1]
      if (scenes.length >= 4) type = scenes[2]
      if (scenes.length >= 4) gid = scenes[3]
      oid = oid.replace("null", '')
      if (!parseInt(oid)) oid = ""
    }
    var pages = getCurrentPages();
    var m = wx.getMenuButtonBoundingClientRect()
    t.setData({
      acttype: e.acttype || 0,
      gid: gid,
      oid: oid,
      type: e.type || type || '',
      history: pages.length > 1,
      mtop: m.top * 2
    })
    $.initparam(t, e, "/pages/" + $.pp(type) + "/" + $.pp(type), gid)
    t.getList()
  },
  onshare: function (t, r) {
    var td = t.data, sharewa = td.sharewa, g = td.g, shareimg = td.shareimg || g.pic, ty = td.type, pt = $.pt(ty),
          pn = $.pn(ty),
          o = $.gcache('guid') + '_' + pt + '_' + r.from + '_' + td.gid,
          title = '好友' + td.nickname + '邀请你一起参与' + td.gname + pn + '活动',
          path = '/pages/' + pt + '/' + pt + '?gid=' + td.gid + '&ori=' + o + '&type=' + ty
                + '&oid=' + td.oid + '&ptoid=' + td.ptoid + '&ts=' + $.ts()
    if (g.status == 1 || g.status == -1) return;
    var share = {
      title: sharewa.length > 0 ? sharewa : title,
      path: path,
      success: function (e) {
        $.shareWithTicket(e, path)
      }
    }
    if (shareimg.length > 0) share.imageUrl = shareimg
    return share;
  }
}
